/**
 * 
 */
package com.peter.website.controller;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Optional;

import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.peter.data.entity.Idea;
import com.peter.data.entity.IdeaTag;
import com.peter.store.idea.IdeaRepository;
import com.peter.store.idea.TagRepository;

import lombok.extern.slf4j.Slf4j;

/**
 * @author peter
 *
 */
@RestController
@Slf4j
@RequestMapping("/rest/idea")
public class IdeaController {
    @Autowired
    private IdeaRepository ideaRepository;
    @Autowired
    private TagRepository tagRepository;

    // MEMO 通过get方式的"?key=value"的方式传递参数。
    @GetMapping("/list")
    public List<Idea> list(@RequestParam(name = "family", required = false, defaultValue = "") String family, Model model) {
        List<Idea> ideas = new ArrayList<Idea>();
        Iterable<Idea> iterator = ideaRepository.findAll();

        // MEMO 通过注解 ``@Slf4j`` 可以直接使用 ``log`` 对象。
        log.info("进入该方法。");

        for(Idea idea: iterator) {
            ideas.add(idea);
        }

        return ideas;
    }

    // MEMO 通过get的"/path/{value}"方式传递参数。
    @GetMapping("/info/{id}")
    public Idea info(@PathVariable(name = "id", required = false) String id, Model model) {
        Optional<Idea> optIdea = ideaRepository.findById(Long.parseLong(id));
        // MEMO Optional.get()之前需要判断是否为空，否则会报错。

        return optIdea.isEmpty() ? null : optIdea.get();
    }

    // MEMO 通过Post方式传递的json参数。
    @RequestMapping(path = "/add", method = {RequestMethod.POST, RequestMethod.PUT})
    public String add(@RequestBody @Valid Idea idea) {
        return "OK";
    }

    // MEMO 通过Post方式传递的json参数。
    @RequestMapping(path = "/test", method = {RequestMethod.POST, RequestMethod.PUT, RequestMethod.GET})
    public Integer test() {
        return 1;
    }

    /**
     * 初始操作，创建初始数据。
     */
    @GetMapping("/opt")
    public Boolean opt() {
        IdeaTag tag1 = new IdeaTag();
        tag1.setName("tag1");
        tagRepository.save(tag1);
        IdeaTag tag2 = new IdeaTag();
        tag2.setName("tag2");
        tagRepository.save(tag2);
        IdeaTag tag3 = new IdeaTag();
        tag3.setName("tag3");
        tagRepository.save(tag3);
        Idea idea1 = new Idea();
        idea1.setContent("this is a test for idea");
        idea1.setFamily("test");
        idea1.setTags(new ArrayList<IdeaTag>());
        idea1.getTags().add(tag1);
        idea1.setTime(new Date());
        ideaRepository.save(idea1);
        Idea idea2 = new Idea();
        idea2.setContent("this is a test for idea");
        idea2.setFamily("test");
        idea2.setTags(new ArrayList<IdeaTag>());
        idea2.getTags().add(tag2);
        idea2.setTime(new Date());
        ideaRepository.save(idea2);
        Idea idea3 = new Idea();
        idea3.setContent("this is a test for idea");
        idea3.setFamily("test");
        idea3.setTags(new ArrayList<IdeaTag>());
        idea3.getTags().add(tag3);
        idea3.setTime(new Date());
        ideaRepository.save(idea3);
        Idea idea4 = new Idea();
        idea4.setContent("this is a test for idea");
        idea4.setFamily("test");
        idea4.setTags(new ArrayList<IdeaTag>());
        idea4.getTags().add(tag1);
        idea4.setTime(new Date());
        ideaRepository.save(idea4);
        return true;
    }
}
